package com.panda.mall.common.base;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;

/**
 * author: tcy
 * createDate: 2023/6/25
 * description: 分页查询基础字段
 */
@Data
public class BasePageQuery {

    @ApiModelProperty(value = "起始页码")
    private Integer pageNum = 1;

    @ApiModelProperty(value = "每页数量")
    private Integer pageSize = 10;

    @ApiModelProperty("排序方式：true-倒序, false-顺序")
    private Boolean sortType = false;

    @ApiModelProperty("排序字段")
    private String sortColumn;

    public List<OrderItem> getOrder() {
        //转换为数据库列字段:sortColumn ——> sort_column
        String column = StrUtil.toUnderlineCase(this.sortColumn);
        if (StrUtil.isNotBlank(column)) {
            return this.sortType ? OrderItem.descs(column) : OrderItem.ascs(column);
        } else {
            return this.sortType ? OrderItem.descs("create_time") : OrderItem.ascs("create_time");
        }
    }
}
